library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;


entity led_updater IS
    GENERIC(
       mem_addr      : std_logic_vector(17 downto 0) := "000000000001111111";
       slowness      : natural := 20
    );
    PORT( 
       clk                : IN std_logic;  
       
       grant              : IN std_logic;  
       grant_valid        : IN std_logic;
       
       sram_read_addr     : in std_logic_vector(17 DOWNTO 0);
       sram_read_data     : in std_logic_vector(15 DOWNTO 0);
       sram_read_valid    : IN std_logic;
       
       
       request_out      : out std_logic;
       request_addr     : out std_logic_vector(17 DOWNTO 0);
       
       led_value        : out std_logic_vector(9 downto 0)
          
    );
end led_updater;



architecture Behavioral of led_updater is
   
  signal slow_counter : unsigned(slowness-1 downto 0) := (others => '0');
   
  signal value_buffer : std_logic_vector(9 downto 0) := "1010111010";
   
  TYPE STATE_TYPE IS (
      wait_slow,
      request,
      granted
   );
 
   SIGNAL state : STATE_TYPE := wait_slow; 
    
begin
        

 
process (clk)
begin
  if rising_edge(clk) then
    
    slow_counter <= slow_counter +1;
    
    case state is
    
      when wait_slow =>     
        if slow_counter=0 then
          state<=request;  
        end if;
        
      when request =>
        if grant='1' and grant_valid='1' then
          state<=granted;  
        end if;
      
      when granted =>
        if sram_read_addr=mem_addr and sram_read_valid='1' then
          state<=wait_slow; 
          value_buffer <= sram_read_data(9 downto 0);
        end if;
    
    end case;
    
  end if;
end process;


  request_out <= '1' when state=request else '0';
  request_addr <= mem_addr;
  
  led_value <= value_buffer;

end Behavioral;












